package com.blink.optimize.apis.collect;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * <Description> </Description>
 * <ClassName> TestCompareLinkedHashMapAndTreeMap</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月08日 13:17
 */
public class TestCompareLinkedHashMapAndTreeMap {
    @Test
    public void testLinkedHashMap() {

    }

    @Test
    public void testTreeMap() {
        Student s1 = new Student("Uzumaki Naruto", 61);
        Student s2 = new Student("Uchiha Sasuke", 98);
        Student s3 = new Student("Senjo Hashirama", 100);
        Student s4 = new Student("Uchiha Madera", 99);
        TreeMap<Student, StudentDetailInfo> studentDetailInfoTreeMap = new TreeMap<>();
        studentDetailInfoTreeMap.put(s1, new StudentDetailInfo(s1));
        studentDetailInfoTreeMap.put(s2, new StudentDetailInfo(s2));
        studentDetailInfoTreeMap.put(s3, new StudentDetailInfo(s3));
        studentDetailInfoTreeMap.put(s4, new StudentDetailInfo(s4));
        //打印分数位于s2和s3之间的人
        System.out.println("打印分数位于s2和s3之间的人:");
        SortedMap<Student, StudentDetailInfo> subMap1 = studentDetailInfoTreeMap.subMap(s2, true, s3, true);
        for (Map.Entry<Student, StudentDetailInfo> entry : subMap1.entrySet()) {
            System.out.println(entry.getKey());
        }
        //打印分数比s2低的人
        System.out.println("打印分数比s2低的人:");
        SortedMap<Student, StudentDetailInfo> subMap2 = studentDetailInfoTreeMap.headMap(s2, false);
        for (Map.Entry<Student, StudentDetailInfo> entry : subMap2.entrySet()) {
            System.out.println(entry.getKey());
        }
        //打印分数比s2低的人
        System.out.println("打印分数比s2高的人:");
        SortedMap<Student, StudentDetailInfo> subMap3 = studentDetailInfoTreeMap.tailMap(s2, false);
        for (Map.Entry<Student, StudentDetailInfo> entry : subMap3.entrySet()) {
            System.out.println(entry.getKey());
        }
    }

    class StudentDetailInfo {
        private Student student;

        public StudentDetailInfo(Student student) {
            this.student = student;
        }

        public Student getStudent() {
            return student;
        }

        public void setStudent(Student student) {
            this.student = student;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("StudentDetailInfo{");
            sb.append("student=").append(student);
            sb.append('}');
            return sb.toString();
        }
    }

    class Student implements Comparable<Student> {
        private String name;
        private int score;

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }

        @Override
        public int compareTo(Student o) {
            return Integer.compare(this.score, o.score);
        }

        @Override
        public String toString() {
            return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
        }
    }
}
